﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wormhole.Engine.Communication
{
	/// <author>Hannes Foulds</author>
	/// <date>01 September 2008</date>
	/// <summary>
	/// This class represents a deck of cards to be synced with the other player.
	/// </summary>
	public class Deck : List<Card>
	{
		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public Deck()
		{
		}

		/// <summary>
		/// Initialize a new instance of the claas from the its string representation.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		public Deck(string message)
		{
			this.Parse(message);
		}
		#endregion

		#region To String
		/// <summary>
		/// Return a string representation of the deck that can be send over the communications channels.
		/// </summary>
		/// <returns>Returns the string representation of the deck.</returns>
		public override string ToString()
		{
			StringBuilder result = new StringBuilder();

			foreach (Card card in this)
			{
				// add the card
				result.Append(card.ToString());
				
				// add the seperator if it is not the last card
				if (card != this.Last())
					result.Append(";");
			}

			return result.ToString();
		}
		#endregion

		#region Parse
		/// <summary>
		/// Parse the string representation of the class.
		/// </summary>
		/// <param name="message">The string message to parse.</param>
		protected void Parse(string message)
		{
			string[] parts = message.Split(';');

			foreach (string part in parts)
			{
				Card card = new Card(part);
				this.Add(card);
			}
		}
		#endregion
	}
}
